package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.RoleOptionsVo;
import com.cskaoyan.catmall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 创建日期: 2022-01-09-08-23
 * author : WQW
 * description: 角色相关的增删改查
 */

import com.cskaoyan.catmall.dao.RoleDao;
import com.cskaoyan.catmall.model.bo.PermissionBo;
import com.cskaoyan.catmall.model.bo.RoleBo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.PermissionInfoVo;
import com.cskaoyan.catmall.model.vo.RoleVo;
import com.cskaoyan.catmall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;

/**
 * @Author: 逍遥子
 * @Date: 2022/01/08/18:34
 * @Description:
 */

@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;


    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 获取所有角色信息
     * @author WQW
     * @date 2022/1/9 10:21
     */
    @GetMapping("options")
    public BaseRespVo options() {
        RoleOptionsVo allRole = roleService.getAllRole();
        return BaseRespVo.ok(allRole);
    }

    /**
     * @param
     * @param roleBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 查看角色
     * @author czx
     * @date 2022-01-08 20:06
     */
    @GetMapping("list")
    public BaseRespVo list(RoleBo roleBo) {

        RoleVo roleVo = roleService.selectRole(roleBo);
        return BaseRespVo.ok(roleVo);

    }
    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 创建角色
     * @author czx
     * @date 2022-01-08 20:08
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody RoleBo roleBo) {

        int res = roleService.insertRole(roleBo);

        if (res == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail();
        }

    }
    /**
     * @param
     * @param roleBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 删除角色
     * @author czx
     * @date 2022-01-08 22:48
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody RoleBo roleBo) {

        int res = roleService.deleteRole(roleBo);

        if (res == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail();
        }

    }
    /**
     * @param
     * @param permissionBo
     * @param roleId
     * @param request
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 角色授权
     * @author czx
     * @date 2022-01-08 21:18
     */
    @GetMapping("permissions")
    public BaseRespVo permissions(PermissionBo permissionBo) {
        PermissionInfoVo permissionInfoVo = null;
        permissionInfoVo = roleService.selectPermission(permissionBo.getRoleId());
        return BaseRespVo.ok(permissionInfoVo);
    }

    @PostMapping("permissions")
    public BaseRespVo permissionPost(@RequestBody PermissionBo permissionBo){
        roleService.updatePermission(permissionBo);
        return BaseRespVo.ok();
    }

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 更新角色信息
     * @author czx
     * @date 2022-01-08 22:51
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody RoleBo roleBo) {
        roleService.updateRoleInfo(roleBo);
        return BaseRespVo.ok();
    }

}
